package cn.yeyiu.util;

import android.content.Context;
import android.content.SharedPreferences;
import cn.yeyiu.util.bean.TimeVideoInfo;
import cn.yeyiu.util.bean.VideoInfo;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class SharedPreferrncesUtil {
    private static SharedPreferences favSharedPreferences = null;
    private static List<TimeVideoInfo> favVideoInfos = null;
    private static final int maxRecSize = 20;
    private static SharedPreferences recSharedPreferences;
    private static List<TimeVideoInfo> recVideoInfos;

    public void spInit(Context context) {
        initRecList(context.getSharedPreferences("recent_videos", 0));
        initFavList(context.getSharedPreferences("favour_videos", 0));
    }

    public static void initRecList(SharedPreferences sharedPreferences) {
        recSharedPreferences = sharedPreferences;
        recVideoInfos = getTimeVideoList(sharedPreferences.getAll());
    }

    public static void initFavList(SharedPreferences sharedPreferences) {
        favSharedPreferences = sharedPreferences;
        favVideoInfos = getTimeVideoList(sharedPreferences.getAll());
    }

    private static List<TimeVideoInfo> getTimeVideoList(Map<String, ?> map) {
        ArrayList arrayList = new ArrayList();
        for (String str : map.keySet()) {
            VideoInfo videoInfo = (VideoInfo) JSONObject.parseObject((String) map.get(str), VideoInfo.class);
            if (videoInfo != null) {
                arrayList.add(new TimeVideoInfo(videoInfo, str));
            }
        }
        arrayList.sort(new Comparator<TimeVideoInfo>() {
            /* class cn.yeyiu.util.SharedPreferrncesUtil.AnonymousClass1 */

            public int compare(TimeVideoInfo timeVideoInfo, TimeVideoInfo timeVideoInfo2) {
                return timeVideoInfo.time.compareTo(timeVideoInfo2.time);
            }
        });
        return arrayList;
    }

    public static void save(VideoInfo videoInfo) {
        SharedPreferences.Editor edit = recSharedPreferences.edit();
        while (recVideoInfos.size() >= 19) {
            edit.remove(recVideoInfos.get(0).time);
            recVideoInfos.remove(0);
        }
        TimeVideoInfo timeVideoInfo = new TimeVideoInfo(videoInfo, String.valueOf(new Date().getTime()));
        edit.putString(timeVideoInfo.time, JSONObject.toJSONString(timeVideoInfo));
        edit.apply();
        recVideoInfos.add(timeVideoInfo);
    }

    public static void like(VideoInfo videoInfo) {
        SharedPreferences.Editor edit = favSharedPreferences.edit();
        for (TimeVideoInfo timeVideoInfo : favVideoInfos) {
            if (timeVideoInfo.title.equals(videoInfo.title)) {
                return;
            }
        }
        TimeVideoInfo timeVideoInfo2 = new TimeVideoInfo(videoInfo, String.valueOf(new Date().getTime()));
        edit.putString(timeVideoInfo2.time, JSONObject.toJSONString(timeVideoInfo2));
        edit.apply();
        favVideoInfos.add(timeVideoInfo2);
    }

    public static List<TimeVideoInfo> load(String str) {
        if ("recent".equals(str)) {
            return recVideoInfos;
        }
        if ("favour".equals(str)) {
            return favVideoInfos;
        }
        return null;
    }

    public static void remove(String str, int i) {
        if ("recent".equals(str)) {
            SharedPreferences.Editor edit = recSharedPreferences.edit();
            int size = (recVideoInfos.size() - i) - 1;
            edit.remove(recVideoInfos.get(size).time);
            edit.apply();
            recVideoInfos.remove(size);
        } else if ("favour".equals(str)) {
            SharedPreferences.Editor edit2 = favSharedPreferences.edit();
            int size2 = (favVideoInfos.size() - i) - 1;
            edit2.remove(favVideoInfos.get(size2).time);
            edit2.apply();
            favVideoInfos.remove(size2);
        }
    }

    public static int size(String str) {
        if ("recent".equals(str)) {
            return recVideoInfos.size();
        }
        if ("favour".equals(str)) {
            return favVideoInfos.size();
        }
        return 0;
    }
}
